'use strict';

angular.module('myApp.teach_record', [])

.controller('teach_recordCtrl', ['$scope','$http','$filter','breadcrumbData',function($scope,$http,$filter,breadcrumbData) {

    //teacher id
    var teacher_id = kget('id');

    
    

    //get classes 
    $http.get('v1/teacher/'+teacher_id+'/classes').success(function(data){
        if(!data.error){
            for(var i=0;i<data.result.length;i++){
                data.result[i].name = data.result[i].school_name+" "+data.result[i].name; 
            }

            $scope.classes = data.result;
            $scope.class = $scope.classes[0];
            
            $scope.classChange();
        }
    });

    //start a new record
    $scope.classChange = function(back){
        $scope.isShow = false;
        $scope.couldEdit = false;
        //default record
        $scope.record = {date:$filter('date')(new Date(), 'yyyy-MM-dd'),lesson_num:1,classes_id:$scope.class.id};
        breadcrumbData[3] = {name:$scope.class.name};

        //students
        $http.get('v1/classes/'+$scope.record.classes_id+'/student').success(function(data){
            if(!data.error){
                $scope.record.student = [];
                for(var i=0;i<data.result.length;i++){
                    $scope.record.student[i] = {
                        student_id : data.result[i].id,
                        lesson_num : 1,
                        is_attend  : true
                    }
                }
                $scope.students = data.result;
            }else{
               $scope.students = []; 
            }
        });

        //teach plan
        $http.get('v1/classes/'+$scope.record.classes_id+'/teach_plan').success(function(data){
            if(!data.error){
                $scope.teach_plans = [];
                for(var i=0;i<data.result.length;i++){
                    $scope.teach_plans[i] = {
                        teach_plan_id : data.result[i].id,
                        plan_name     : data.result[i].plan_name,
                        master_level  : 3
                    }
                }
                
            }else{
                $scope.teach_plans = [];
            }
        });

        //history
        $http.get('v1/teacher/'+teacher_id+'/record?classes_id='+$scope.record.classes_id).success(function(data){
           
            if(!data.error){
                $scope.historys = [];
                var length = data.result.data.length;

                for(var i=0;i<length;i++){
                    $scope.historys[i+1] = {
                        id  : data.result.data[i].id,
                        name: "第"+(length-i)+"次 ["+data.result.data[i].date+" "+data.result.data[i].start_time+"]"
                    }
                }
                $scope.historys[0] = {
                    id:0,
                    name: "第"+(length+1)+"次 [创建]"
                }
                
            }else{
                $scope.historys = [];
                $scope.historys[0] = {
                    id:0,
                    name: "第1次 [创建]"
                }
                
            }

            $scope.history = $scope.historys[0];
            breadcrumbData[4] = {name:$scope.history.name};
            
        });
        

    }

    //show a record history
    $scope.historyChange = function(){

        breadcrumbData[4] = {name:$scope.history.name};

        if($scope.history.id == $scope.historys[1].id){
            $scope.couldEdit = true;
        }else{
            $scope.couldEdit = false;
        }
        if($scope.history.id == 0){
            $scope.classChange();
            $scope.isShow = false;
            return;
        }

        $scope.isShow = true;
        //history record
        $http.get('v1/teacher/'+teacher_id+'/record/'+$scope.history.id).success(function(data){
         console.log(data.result);
            if(!data.error){
                data.result.lesson_num *= 1;
                for(var i=0;i<data.result.student.length;i++){
                    data.result.student[i]['lesson_num'] *= 1;
                    data.result.student[i]['is_attend'] = (data.result.student[i]['is_attend']=="1")?true:false;
                    for(var j=0;j<data.result.student[i]['teach_plan'].length;j++){
                        data.result.student[i]['teach_plan'][j]['master_level'] *= 1; 
                    }
                }
                var teach_plans = [];
                for(var i=0;i<$scope.teach_plans.length;i++){
                    for(var j=0;j<data.result.teach_plan.length;j++){
                        if($scope.teach_plans[i]['teach_plan_id'] == data.result.teach_plan[j]['teach_plan_id']){
                            teach_plans.push($scope.teach_plans[i]);
                        }
                    }
                }
                data.result.teach_plan = teach_plans;

                $scope.record = data.result;
            }else{
                $scope.record = {};
            }
        });
        //
    }
    
    //teach_plan copy
    $scope.teach_planChange = function(){
        for(var i=0;i<$scope.students.length;i++){
            $scope.record.student[i].teach_plan = angular.copy($scope.record.teach_plan);
        }
    }

    //lesson_num copy
    $scope.lesson_numChange = function(){
        for(var i=0;i<$scope.students.length;i++){
            $scope.record.student[i].lesson_num = $scope.record.lesson_num; 
        }
    }

    //submit
    $scope.submit = function(){

        if($scope.record.id){
            console.log($scope.record);
            alert("update!");
        }else{
            $http.post('v1/teacher/'+teacher_id+'/record',$scope.record).success(function(data){
                if(!data.error){
                    alert(data.message,"success");
                }else{
                    alert(data.message);
                }
            });           
        }
       
    }
    $scope.delete = function(){
        alert("delete");
    }

    //date time picker
    $('#date').datetimepicker({
        language:  'zh-CN',
        weekStart: 1,
        todayBtn:  1,
        autoclose: 1,
        todayHighlight: 1,
        startView: 2,
        minView: 2,
        forceParse: 0,
        startDate: new Date(),
        format: 'yyyy-mm-dd'
    });
    $('#start_time,#end_time').datetimepicker({
        language:  'zh-CN',
        weekStart: 1,
        todayBtn:  1,
        autoclose: 1,
        todayHighlight: 1,
        startView: 1,
        minView: 0,
        maxView: 1,
        forceParse: 0,
        format: 'hh:ii'
    });

	

}]);